﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKSparseDistance.ModelLayer;

namespace YJKSparseDistance.ControlLayer
{
	internal class ShortDistacneCalculateBean
	{
		public ShortDistacneCalculateBean(EndPointPacker startPoint)
		{
			this.m_StartPoint = startPoint;
		}

		public void AddEndPoint(EndPointPacker endPoint, List<Curve> runWayCurve)
		{
			if (!this.m_EndPointAndWays.ContainsKey(endPoint))
			{
				this.m_EndPointAndWays.Add(endPoint, runWayCurve);
			}
		}

		public List<ResultBean> LstResultBean
		{
			get
			{
				return this.m_lstResultBean;
			}
			private set
			{
				this.m_lstResultBean = value;
			}
		}

		public void Calculate()
		{
			List<XYZ> list = new List<XYZ>();
			new List<Curve>();
			foreach (KeyValuePair<EndPointPacker, List<Curve>> keyValuePair in this.m_EndPointAndWays)
			{
				list = this.m_useCalculater.CalculateShortPath(this.m_StartPoint, keyValuePair.Key, keyValuePair.Value);
				if (list != null)
				{
					this.LstResultBean.Add(new ResultBean(this.m_StartPoint, keyValuePair.Key, list));
				}
			}
			if (this.LstResultBean.Count != 0)
			{
				this.SetFinalEnd();
			}
			for (int i = 0; i < this.LstResultBean.Count; i++)
			{
				this.LstResultBean[i].UseRule = EvacuationDistanceArch.UesRulePacker;
			}
		}

		private void SetFinalEnd()
		{
			int useIndex = 0;
			if (this.m_StartPoint.AppendPoint != null)
			{
				useIndex = 1;
			}
			double num = Utility.GetFullAngleToBasisX(this.LstResultBean[0], useIndex);
			double num2 = Utility.GetFullAngleToBasisX(this.LstResultBean[0], useIndex);
			foreach (ResultBean inputValue in this.LstResultBean)
			{
				double fullAngleToBasisX = Utility.GetFullAngleToBasisX(inputValue, useIndex);
				if (fullAngleToBasisX < num)
				{
					num = fullAngleToBasisX;
				}
				else if (fullAngleToBasisX > num2)
				{
					num2 = fullAngleToBasisX;
				}
			}
			if (Math.PI*.5 > num2 - num)
			{
				this.m_StartPoint.IfFinalEnd = true;
			}
		}

		private EndPointPacker m_StartPoint;

		private Dictionary<EndPointPacker, List<Curve>> m_EndPointAndWays = new Dictionary<EndPointPacker, List<Curve>>();

		private ShortDistanceCalculater m_useCalculater = new ShortDistanceCalculater();

		private List<ResultBean> m_lstResultBean = new List<ResultBean>();
	}
}
